package com.hbxs.folder.api;

import com.hbxs.folder.bean.CustomGenericException;
import com.hbxs.folder.bean.Result;
import com.hbxs.folder.logic.AnnexLogic;
import com.hbxs.folder.pojo.query.IdQuery;
import com.hbxs.folder.pojo.vo.FolderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("annex")
@Api(value="附件接口", tags="附件接口")
public class AnnexApi extends BaseApi {

    @Autowired
    private AnnexLogic annexLogic;

    @PostMapping(value = "upload",consumes = "multipart/*",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "附件上传",produces="multipart/form-data")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "文件组(在UI界面端,按住Ctrl键即可多选文件)",allowMultiple=true, required = true, dataType = "MultipartFile", paramType = "form"),
    })
    public Result<List<FolderVO>> upload(HttpServletRequest request, @RequestParam @NotNull(message = "文件不为空") List<MultipartFile> files) throws IOException {
        return annexLogic.upload(request,files);
    }

    @GetMapping("data/{id}")
    @ApiOperation(value = "获取文件")
    @ApiImplicitParam(name = "id", value = "文件id", required = true, dataType = "string", paramType = "path")
    public void getData(@PathVariable @NotNull(message = "id不能为空") String id, HttpServletResponse response) throws IOException, CustomGenericException {
        annexLogic.getData(id, response);
    }

    @GetMapping("getDataBreakpoint/{id}")
    @ApiOperation(value = "获取文件断点")
    @ApiImplicitParam(name = "id", value = "文件id", required = true, dataType = "string", paramType = "path")
    public void getDataBreakpoint(@PathVariable @NotNull(message = "id不能为空") String id, HttpServletResponse response, @RequestHeader(required = false) String range) throws IOException, CustomGenericException {
        annexLogic.getDataBreakpoint(id, response, range);
    }

    @PostMapping("del")
    @ApiOperation(value = "删除文件")
    public Result<String> del(@RequestBody @Valid IdQuery idQuery){
        return annexLogic.del(idQuery.getId());
    }
}
